import { getAST, coreRemark } from '../remark.js';
/**
 * if a string has a paragraph as its outermost element, remove it (e.g. `<p><code>hello</code></p>` -> `<code>hello</code>`)
 * in AST form it looks like:
 * {
 *  type: 'root',
 *  children: [
 *  {
 *    type: 'paragraph',
 *    children: [
 *      {
 *        type: 'mdxJsxTextElement',
 *        name: 'p',
 *        children: [<the content>]
 *      }
 *     ]
 *   }]
 * }
 * @param content mdx string
 * @returns mdx string without outer paragraph
 */
export const removeOuterParagraph = (content) => {
    var _a, _b, _c, _d, _e;
    const ast = getAST(content);
    if (ast.children.length === 1 && ((_a = ast.children[0]) === null || _a === void 0 ? void 0 : _a.type) === 'paragraph') {
        ast.children = (_b = ast.children[0]) === null || _b === void 0 ? void 0 : _b.children;
    }
    if (ast.children.length === 1 &&
        ((_c = ast.children[0]) === null || _c === void 0 ? void 0 : _c.type) === 'mdxJsxTextElement' &&
        ((_d = ast.children[0]) === null || _d === void 0 ? void 0 : _d.name) === 'p') {
        ast.children = (_e = ast.children[0]) === null || _e === void 0 ? void 0 : _e.children;
    }
    const stringified = coreRemark().stringify(ast);
    return stringified;
};
